package cn.ekuma.epos.datalogic.define.dao.erp.subject;

import com.openbravo.bean.BusinessPartner;
import com.openbravo.bean.erp.subject.Subject;
import com.openbravo.bean.erp.subject.SubjectCustomer;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.KeyBuilder;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;
import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.data.dao.bean.I_ModifiedLogBean;
import cn.ekuma.epos.datalogic.define.dao.CustomerInfoDAO;

public class SubjectCustomerDAO extends ModifiedLogDAO<SubjectCustomer>{
	CustomerInfoDAO customerInfoDAO;
	
	public SubjectCustomerDAO(I_Session s,CustomerInfoDAO customerInfoDAO) {
		super(s);
		this.customerInfoDAO=customerInfoDAO;
	}

	public SubjectCustomerDAO(I_Session s) {
		super(s);
	}

	@Override
	public SubjectCustomer readValues(DataRead dr,SubjectCustomer obj) throws BasicException {
		if(obj==null)
			obj=new SubjectCustomer();
		obj.setId(dr.getString(1));
		obj.setSubjectId(dr.getString(2));
		obj.setCustomerID(dr.getString(3));
		if(customerInfoDAO!=null)
		   obj.setCustomer(customerInfoDAO.find(KeyBuilder.getKey(dr.getString(3))));
		obj.setLastModified(dr.getTimestamp(4));
		return obj;
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s,
                "SUBJECT_CUSTOMER",
                new Field[]{
                   new Field("ID",Datas.STRING, Formats.STRING),
                   new Field("SUBJECT",Datas.STRING, Formats.STRING,Subject.class),
                   new Field("CUSTOMER",Datas.STRING, Formats.STRING,BusinessPartner.class),
                   new Field(I_ModifiedLogBean.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP)
                 },
                new int[]{0}
                );
	}

	@Override
	public void writeInsertValues(DataWrite dp, SubjectCustomer obj)
			throws BasicException {
		dp.setString(1, obj.getId());
		dp.setString(2,obj.getSubjectId());
		dp.setString(3,obj.getCustomerID());
		dp.setTimestamp(4,obj.getLastModified());
	}

	@Override
	public Class getSuportClass() {
		return SubjectCustomer.class;
	}

}
